/*
*Copyright 2021-2023 NERCIS
*
*Licensed under the Apache License, Version 2.0 (the "License");
*you may not use this file except in compliance with the License.
*You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*Unless required by applicable law or agreed to in writing, software
*distributed under the License is distributed on an "AS IS" BASIS,
*WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*See the License for the specific language governing permissions and
*limitations under the License.
*/

package cn.ac.nercis.pes.community.component.spreadsheet;

import javafx.collections.ObservableList;
import javafx.scene.control.ComboBox;
import javafx.scene.input.KeyCode;
import org.apache.commons.lang3.StringUtils;
import org.controlsfx.control.spreadsheet.SpreadsheetCellEditor;
import org.controlsfx.control.spreadsheet.SpreadsheetView;

import java.util.Objects;

/**
 * 单元格列表编辑器控件
 * @author zpy
 * @version 1.0.0
 */
public class PesComboBoxCellEditor<T> extends SpreadsheetCellEditor {
    private final ObservableList<T> itemList;
    private final ComboBox<T> cb;
    private String originalValue;

    /**
     * Constructor for the ListEditor.
     * @param view The SpreadsheetView
     * @param itemList The items to display in the editor.
     */
    public PesComboBoxCellEditor(SpreadsheetView view, final ObservableList<T> itemList) {
        super(view);
        this.itemList = itemList;
        cb = new ComboBox<>();
        cb.getStyleClass().add("pes-combo-box");
        cb.setVisibleRowCount(5);
    }
    @Override
    public void startEdit(Object value, String format, Object... options) {
        if (Objects.nonNull(value)) {
            originalValue = value.toString();
        } else {
            originalValue = null;
        }
        cb.setItems(itemList);
        itemList.stream()
                .filter(item-> StringUtils.equals(item.toString(),originalValue))
                .findFirst()
                .ifPresent(cb::setValue);

        attachEnterEscapeEventHandler();
        cb.show();
        cb.requestFocus();
    }

    @Override
    public void end() {
        cb.setOnKeyPressed(null);
    }

    @Override
    public ComboBox<T> getEditor() {
        return cb;
    }

    @Override
    public String getControlValue() {
        if(Objects.isNull(cb.getSelectionModel().getSelectedItem())){
            return StringUtils.EMPTY;
        }
        return cb.getSelectionModel().getSelectedItem().toString();
    }

    public T getValue(){
        return cb.getSelectionModel().getSelectedItem();
    }
    private void attachEnterEscapeEventHandler() {
        cb.setOnKeyPressed(t -> {
            if (t.getCode() == KeyCode.ESCAPE) {
                itemList.stream()
                        .filter(item-> StringUtils.equals(item.toString(),originalValue))
                        .findFirst()
                        .ifPresent(cb::setValue);
                endEdit(false);
            } else if (t.getCode() == KeyCode.ENTER) {
                endEdit(true);
            }
        });
    }
}